package com.xie.controller;

import com.xie.pojo.Task;
import com.xie.service.TaskService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/task")
public class TaskController {

    @Autowired
    @Qualifier("TaskServiceImpl")
    private TaskService taskService;

    @RequestMapping("/allTask")
    public String allTask(HttpSession session){
        List<Task> tasks = taskService.queryAllTask();
        session.setAttribute("taskList",tasks);
        return "task";
    }
    @RequestMapping("/allTasks")
    public String allTasks(HttpSession session){
        List<Task> tasks = taskService.queryAllTask();
        session.setAttribute("taskList",tasks);
        return "tasks";
    }


    @RequestMapping("/toAddTask")
    public String toAddTask(){
        return "addTask";
    }
    @RequestMapping("addTask")
    public String addTask(Task task,Model model){
        int i = taskService.addTask(task);
        if (i > 0){
            return "redirect:/task/allTask";
        }else{
            model.addAttribute("msg","添加失败");
            return "addTask";
        }
    }

    @RequestMapping("/deleteTask")
    public String deleteTask(int task_id, Model model){
        int i = taskService.deleteTask(task_id);
        if(i > 0){
            return "redirect:/task/allTask";
        }else{
            model.addAttribute("msg","删除失败");
            return "redirect:/task/allTask";
        }
    }

    @RequestMapping("/toUpdateTask")
    public String toUpdateTask(int task_id,Model model){
        Task task = taskService.queryTask(task_id);
        model.addAttribute("QTask",task);
        return "updateTask";
    }
    @RequestMapping("updateTask")
    public String updateTask(Task task,Model model){
        int i = taskService.updateTask(task);
        if(i > 0){
            return "redirect:/task/allTask";
        }else{
            model.addAttribute("msg","修改失败");
            return "redirect:/task/allTask";
        }
    }

    @RequestMapping("/toAddHomeworks")
    public String toAddHomeworks(int task_id,HttpSession session){
        Task task = taskService.queryTask(task_id);
        session.setAttribute("tasks",task);
        return "redirect:/homework/toAddHomeworks";
    }

    @RequestMapping("/excelTest")
    public String excelTest(int task_id, Model model, HttpServletRequest req) throws IOException {
        Task task = taskService.queryTask(task_id);
        //路径
        String Path = "D:\\ideawork\\homework\\web\\upload\\";
//        String Path2 = req.getSession().getServletContext().getRealPath("/upload");
        //创建工作簿
        Workbook workbook = new XSSFWorkbook();
        //创建工作表
        Sheet sheet = workbook.createSheet();
        //创建行
        Row rowTitle = sheet.createRow(0);
        //创建列
        Cell cellTitle1 = rowTitle.createCell(0);
        cellTitle1.setCellValue("作业任务id");
        Cell cellTitle2 = rowTitle.createCell(1);
        cellTitle2.setCellValue("作业任务名字");
        Cell cellTitle3 = rowTitle.createCell(2);
        cellTitle3.setCellValue("开始日期");
        Cell cellTitle4 = rowTitle.createCell(3);
        cellTitle4.setCellValue("截止日期");
        Cell cellTitle5 = rowTitle.createCell(4);
        cellTitle5.setCellValue("问题");
        Cell cellTitle6 = rowTitle.createCell(5);
        cellTitle6.setCellValue("附件");
        Cell cellTitle7 = rowTitle.createCell(6);
        cellTitle7.setCellValue("课程");

        //创建行
        Row rowTitleS = sheet.createRow(1);
        //创建列
        Cell cellTitle1S = rowTitleS.createCell(0);
        cellTitle1S.setCellValue(task.getTask_id());
        Cell cellTitle2S = rowTitleS.createCell(1);
        cellTitle2S.setCellValue(task.getTask_name());

        Cell cellTitle3S = rowTitleS.createCell(2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 日期格式: yyyy-MM-dd 年月日  yyyy-MM-dd HH:mm:ss 年月日 时分秒
        String stime = sdf.format(task.getStart_time());// 这步根据具体的业务来确定
        cellTitle3S.setCellValue(stime);

        Cell cellTitle4S = rowTitleS.createCell(3);
        String ttime = sdf.format(task.getStop_time());
        cellTitle4S.setCellValue(ttime);

        Cell cellTitle5S = rowTitleS.createCell(4);
        cellTitle5S.setCellValue(task.getText_content());
        Cell cellTitle6S = rowTitleS.createCell(5);
        cellTitle6S.setCellValue(task.getTipss());
        Cell cellTitle7S = rowTitleS.createCell(6);
        cellTitle7S.setCellValue(task.getCname());


        FileOutputStream fileOutputStream = new FileOutputStream(Path + task.getTask_name() + ".xlsx");

        workbook.write(fileOutputStream);


        fileOutputStream.close();

        return "redirect:/task/allTask";
    }









}
